<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {
	
	/** 
	 *	Register page for growers
	 *  种植户注册页
	 */

	public function __construct() {
		parent::__construct();
		$this->load->model('options_model');
		$this->load->model('register_model');
		$this->load->library('form_validation');
	}
	
/*	public function index() {

		if (!isset($_COOKIE['token'])) {
		$this->load->helper('string');
		$this->load->helper('cookie');
		$cookie = array(
			'name' => 'token',
			'value' => random_string('alpha', 16),
			'domain' => '.darkstar.com'
		);
		$this->input->set_cookie($cookie);
		}
		

		$header_data['title'] = '注册';
		$base_url = base_url();
		$header_data['script']=<<<EOD
		<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
		<script src="http://darkstar.com/codeigniter/js/jquery.cityselect.js"></script>
		<script type="text/javascript">
			$(function() {
				$("#administrative").citySelect({
					nodata:"none",
					required:false
				});
			});
		</script>
EOD;
		$this->load->view('templates/header', $header_data);
		$this->load->view('register/index');
		$this->load->view('templates/footer');
	}
*/
	public function index() {
		$this->form_validation->set_rules('station', '试验站', 'required');
		$this->form_validation->set_rules('prov', '省/直辖市/自治区', 'required');
		$this->form_validation->set_rules('city', '市/区/自治州', 'required');
		$this->form_validation->set_rules('dist', '县/市/区', 'required');
		$this->form_validation->set_rules('address', '详细地址', 'required');
		$this->form_validation->set_rules('contact', '联系人', 'required|min_length[2]|callback__validate_all_ch');
		$this->form_validation->set_rules('telephone', '手机', 'callback__validate_tel');
		
		
		$input_id_need_bubble = array('address_input', 'contact_input', 'telephone_input');
		
		if ($this->form_validation->run() == FALSE) { //验证未通过
			$header_data['title'] = '注册';
			
			// 设置提示气泡的JS脚本
			$bubbles_js['input_id'] = $input_id_need_bubble;
			$data['script_tooltip'] = $this->load->view('scripts/tooltip', $bubbles_js, TRUE);
			
			// 载入视图
			$this->load->view('templates/header', $header_data);
			$this->load->view('register/index', $data);
			$this->load->view('templates/footer');
			$this->load->view('templates/debug');
		} 
		else { // 验证通过
			$header_data['title'] = "Debug";
			
			$this->load->view('templates/header', $header_data);
			$this->load->view('register/new_vineyard');
			$this->load->view('templates/footer');
			$this->register_model->set_register();
		}
	}

	//检测所有字符都必须为中文，例如：姓名项
	function _validate_all_ch($str) {
		if (! preg_match("/^[\x{4e00}-\x{9fa5}]+$/u", $str)) {
			return FALSE;
		}
		return TRUE;
	}


	//检测手机号码输入栏
	function _validate_tel($str) {
		//输入栏为空
		if (trim($str) == '') { 
			$this->form_validation->set_message('_validate_tel', "请填入您的手机号码");
			return FALSE;
		}
		//手机号的正则匹配
		else if ( ! preg_match("/13[0-9]{9}|15[0|1|2|3|5|6|7|8|9]\d{8}|18[0|5|6|7|8|9]\d{8}/", $str)) {
			$this->form_validation->set_message('_validate_tel', "请输入正确的手机号码");
			return FALSE;
		}
		return TRUE;
	}

}